﻿using BLL;
using Microsoft.VisualBasic.ApplicationServices;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Tools;
using 后整理管理系统.其他窗体;
using 纺织贸易管理系统;
using 纺织贸易管理系统.自定义类;
using 纺织贸易管理系统.设置窗体;

namespace 后整理管理系统.报表窗体
{
    public partial class 超期库存 : Sunny.UI.UIForm 
    {
        public 超期库存()
        {
            InitializeComponent();
            uiDatePicker1.Value = DateTime.Today.AddDays(-5);
            CreateGrid.Create(this.Name, gridView1, "uiDataGridView1");
            CreateGrid.Create("原料库存", gridView2, "gridView1");
            gridView2.Columns.Add(new DevExpress.XtraGrid.Columns.GridColumn() { Caption = "进仓数量",FieldName = "进仓数量",Name="进仓数量", Width=50,Visible=true  }) ;
            gridView2.Columns.Add(new DevExpress.XtraGrid.Columns.GridColumn() { Caption = "领用数量", FieldName = "领用数量", Name = "领用数量", Width = 50, Visible = true });
        }

        private void 配置列ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var fm = new 配置列(gridView1) { formname = this.Name, Obj = new StockTable () };
            fm.ShowDialog();
        }

        private void 保存样式ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CreateGrid.SaveGridview(this.Name, gridView1);
        }

        private void 超期库存_Load(object sender, EventArgs e)
        {
            Query();
            try
            {
                gridControl2.DataSource = Connect.GetSugar().Ado.GetDataTable($"SELECT *,(SELECT sum(InventoryQuantity) from ImportAndExportOfRawMaterials ,DanjuTable  WHERE danjutable.djlx ='原料入库单' and danjutable.rq='{DateTime.Today}' and danjutable.dh=ImportAndExportOfRawMaterials.DocNum and ImportAndExportOfRawMaterials.Num=rm.Num) as '进仓数量', (SELECT sum(InventoryQuantity) from ImportAndExportOfRawMaterials ,DanjuTable  WHERE danjutable.djlx ='原料领用单' and danjutable.rq='{DateTime.Today.AddDays(-1)}' and danjutable.dh=ImportAndExportOfRawMaterials.DocNum and ImportAndExportOfRawMaterials.Num=rm.Num) as '领用数量' from RawMaterial rm ");
            }
            catch (Exception ex)
            {

                ShowMessageBox.ShowErrorBox(ex.Message);
            }
        }

        private void 导出到ExcelToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ExportFile.导出到文件(gridControl1, "超期未加工");
        }
        private void Query()
        {
            var items = Connect.GetSugar().Ado.SqlQuery<StockTable>($"select stocktable.* from stocktable , Liuzhuancard where Liuzhuancard.CardNum=stocktable.LiuzhuanCard and LiuzhuanCard.Card_Date<='{uiDatePicker1.Value }' and stocktable.yijianjuanshu =0 and stocktable.yijianmishu =0 order by stocktable.rq desc");
            gridControl1.DataSource = items;
        }
        private void uiSymbolButton1_Click(object sender, EventArgs e)
        {
            Query();
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            Connect.GetSugar().Ado.ExecuteCommand($"update AccessTable set Access='{!checkBox1.Checked}' where AccessName = '超期库存价格可见' and UserID = '{CurrentUser.user.YHBH}'");
        }
    }
}
